﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DTO;
using DataService;

public partial class Admin_Firewalls : BasePage
{
    private int customerId;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (AdminLogin == null)
            Response.Redirect("~/admin/login.aspx");

        Initial();
    }

    public void Initial ()
    {
        if ( !string.IsNullOrEmpty(Request.Params["ac"]) )
        {
            int.TryParse(Request.Params["ac"], out customerId);

            if (customerId == 0)
                Response.Redirect("~/admin/default.aspx");

            using ( DeviceService service = new DeviceService() )
            {
                DisplayFirewalls(service.GetFirewallsByCustomerId(customerId));
            }
        }
    }
    protected void btnAddFirewall_Click ( object sender, EventArgs e )
    {
        Response.Redirect("~/admin/firewall.aspx?c=" + customerId);
    }
    protected void grdFirewalls_RowCommand ( object sender, GridViewCommandEventArgs e )
    {
        int firewallId =Convert.ToInt32( e.CommandArgument);

        if ( firewallId > 0 )
        {
            if (e.CommandName == "EditFirewall")
            {
                Response.Redirect("~/admin/firewall.aspx?c=" + customerId + "&f=" + firewallId);
            }
            else if (e.CommandName == "DeleteFirewall")
            {
                DeleteFirewall(firewallId);
            }
        }
    }
    private void DeleteFirewall ( int firewallId )
    {
        using ( DeviceService service = new DeviceService() )
        {
            service.DeleteFirewall(firewallId);
            DisplayFirewalls(service.GetFirewallsByCustomerId(customerId));
        }        
    }
    private void DisplayFirewalls (IList<Firewall> firewalls)
    {
        using ( DeviceService service = new DeviceService() )
        {
            grdFirewalls.DataSource = firewalls;
            grdFirewalls.DataBind();
        }
    }
}